local json      = require 'cjson.safe'
local utils = require 'utils'

local image_creative_table = {1, 3, 4, 6, 7, 8}
local video_creative_table = {2, 5, 9, 10}

local image_response = {
    sid = "91b5e18c-014c-1000-e71a-3e9942b60043",
    code = 200,
    debuginfo = "SUCCESS",
    tcost = 50,
    bid = {
    {
        impid = "60a9b20ce5ec11e5af66a45e60c539c5",
        adtype = 1,
        creative_type = 1,
        price = 300,
        cid  = "20031",
        crid = "100345",
        title = "native title",
        desc = "native desc",
        image = {"http://www.bayescom.com/images/mainimage.png"},
        nurl = "http://127.0.0.1/nurl?sid=xxximpid=xxx&price=__PRICE__",
        link = "http://www.bayescom.com/landing_page",
        deeplink = "aishangtianqi_tencent:http://www.bayescom.com/deeplink_page",
        action = 1,
        adsource = "baidu",
        imptk = {
              "http://www.bayescom.com/impdisplayreport1",
              "http://www.bayescom.com/impdisplayreport2"
        },
        clicktk = {
              "http://www.bayescom.com/clickdisplayreport1",
              "http://www.bayescom.com/clickdisplayreport2"
        },
        deeplinktk = {
              "http://www.bayescom.com/deeplinkdisplayreport1",
              "http://www.bayescom.com/deeplinkdisplayreport2"
        },
    }
    }
}

local video_response = {
    sid = "91b5e18c-014c-1000-e71a-3e9942b60043",
    code = 200,
    debuginfo = "SUCCESS",
    tcost = 50,
    bid = {
    {
        impid = "60a9b20ce5ec11e5af66a45e60c539c5",
        adtype = 3,
        creative_type = 5,
        title = 'native video title',
        desc = 'native video desc',
        price = 300,
        cid = "20031",
        crid = "100345",
        vurl = "http://www.bayescom.com/video/mainvideo.mp4",
        video_image = "http://www.bayescom.com/video/main.jpg",
        nurl = "http://127.0.0.1:8699/nurl?sid=xxximpid=xxx&price=__PRICE__",
        link = "http://www.bayescom.com/landing_page",
        deeplink = "suliangyigou:http://www.bayescom.com/deeplink_page",
        action = 1,
        adsource = "baidu",
        duration = 15,
        imptk = {
            "http://www.bayescom.com/impdisplayreport1",
            "http://www.bayescom.com/impdisplayreport2"
        },
        clicktk = {
            "http://www.bayescom.com/clickdisplayreport1",
            "http://www.bayescom.com/clickdisplayreport2"
        },
        deeplinktk = {
            "http://www.bayescom.com/deeplinkdisplayreport1",
            "http://www.bayescom.com/deeplinkdisplayreport2"
        },
        starttk = {
            "http://www.bayescom.com/videostartdisplayreport1",
            "http://www.bayescom.com/videostartdisplayreport2"
        },
        midtk = {
            "http://www.bayescom.com/videomiddisplayreport1",
            "http://www.bayescom.com/videomiddisplayreport2"
        },
        endtk = {
            "http://www.bayescom.com/videoenddisplayreport1",
            "http://www.bayescom.com/videoenddisplayreport2"
        },
      }
    }
}


local function handleDsp()
    local path = ngx.var.document_uri
    if path == '/dsp' then
        ngx.req.read_body()
        local request_data = ngx.req.get_body_data()
        local request_json = json.decode(request_data)

        local len = table.getn(request_json.adspot.support_creative_type)
        local location = math.random(1, len)
        local creative_type = request_json.adspot.support_creative_type[location]
        if utils.isInTable(creative_type, image_creative_table) then
            image_response.bid[1].adtype = request_json.adspot.adtype
            image_response.bid[1].creative_type = creative_type
            return json.encode(image_response), 200
        elseif utils.isInTable(creative_type, video_creative_table) then
            video_response.bid[1].adtype = request_json.adspot.adtype
            video_response.bid[1].creative_type = creative_type
            return json.encode(video_response), 200
        end
    elseif path == '/nurl' then
        ngx.log(ngx.ERR, 'get nurl track price succeed')
        return 'OK', 200
    end
end

local function httpd()
    local status, result, code = pcall(
        function ()
            return handleDsp()
        end
    )

    if status then
        ngx.status = code
        ngx.say(result)
        ngx.exit(code)
    else
        ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
        ngx.say("500 INTERNAL_SERVER_ERROR")
        ngx.log(ngx.ERR, result)
        ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
    end
end

httpd()
